
#include <unistd.h>
#include <errno.h>

#include "MobsBuf.h"

int main(int argc,char *argv[])
{
    int ret = 0,woid;
    uintptr_t wuid;
    
    mobs_unlink("/tmp/buf02.mobs");

    char user_name[32];
    snprintf(user_name,sizeof(user_name),"%s",argv[0]);
    if(argc >= 2)
    {
	snprintf(user_name,sizeof(user_name),"prod_%s",argv[1]);
    }
    wuid = mobs_open(user_name,"/tmp/buf02.mobs",
	    MOBS_FLAG_CREATE|MOBS_FLAG_WRONLY|MOBS_FLAG_SHARED,
	    1024*1024,1024,1024);
    printf("open mobs wuid = 0x%08lx\n",wuid);

    if(wuid == -1) exit(EXIT_FAILURE);

    char data[1024]="hello mobs!";
    size_t cnt = 0;
    while(1)
    {//test write
	ret = mobs_start(wuid,0);
	printf("~~~write start ret = %d\n",ret);
	if(ret < 0) exit(EXIT_FAILURE);

	snprintf(data,sizeof(data),"hello mobs - msg %02zu!",cnt++);

	woid = ret;
	ret = mobs_write(wuid,woid,data,strlen(data));
	printf("###write data:%s ret = %d\n",data,ret);
	if(ret < 0)
	{
	    exit(EXIT_FAILURE);
	}
	mobs_finish(wuid,woid);

	sleep(1);
    }
    
    mobs_close(wuid);
    printf("close mobs wuid = 0x%08lx\n",wuid);

    mobs_unlink("/tmp/buf02.mobs");
    return 0;
}

